﻿/*
=======================================================
    Department【部门】
=======================================================
*/

namespace Services
{
    public interface IDepartmentRepository : IRepository<Department>
    {
        int UpdateIsDelete(bool flag, int[] ids);
        Department GetParent(int parentID);
        int GetChildrenCount(int departmentID);
    }


    public class DepartmentRepository : BaseRepository<Department>, IDepartmentRepository
    {
        public DepartmentRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<Department> query)
            : base(dB, currentUser, query)
        {
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, int[] ids)
        {
            return _DB.Updateable<Department>()
                      .SetColumns(x => new Department { IsDelete = flag })
                      .Where(x => ids.Contains(x.DepartmentID))
                      .ExecuteCommand();
        }
        #endregion

        #region GetParent
        public Department GetParent(int parentID)
        {
            return _query.GetQueryable().Where(x => x.DepartmentID == parentID).First();
        }
        #endregion

        #region GetChildrenCount
        public int GetChildrenCount(int departmentID)
        {
            return _query.GetQueryable().Where(x => x.ParentID == departmentID).Count();
        }
        #endregion
    }
}
